package com.sean.ClassLoader;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class CustomerClassLoader extends ClassLoader {

    @Override
    public Class<?> findClass(String name) {
        byte[] bytes = new byte[0];
        try {
            bytes = loadBytefromName(name);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return defineClass(name, bytes, 0, bytes.length);
    }

    private byte[] loadBytefromName(String name) throws IOException {
        ClassLoader thisClassLoader = getClass().getClassLoader();
        System.out.println(thisClassLoader.toString());
        InputStream inputStream = thisClassLoader.getResourceAsStream(name.replace(".", "/") + ".class");
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        int len = 0;
        while ((len = inputStream.read()) != -1) {
            outputStream.write(len);
        }
        return outputStream.toByteArray();
    }


}
